<?php

/*
Various functions
*/

/** redirect_to("/my/index.php");
* Redirects to somewhere, in a manner that *even* MSIE can understand.
* Which takes some doing.
*/
function redirect_to( $location ) {

    $location = preg_replace('!^http://[^/]+!', '', $location);
    // if ( preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT']) ) {
/** <script type='text/javascript' language='javascript'>
        window.location.href= 'http://" .
             $_SERVER['HTTP_HOST'] . $location . "';
             </script></head>";
*/
        echo "<html><head>
<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=http://".
             $_SERVER['HTTP_HOST'] . $location . "\"></head>";
         echo "If you are not immediately redirected, please <a
         href='http://" . 
             $_SERVER['HTTP_HOST'] . $location . "'>click here</a>";
         echo "</html>";
         exit();
    // } else {
        // header("Location: http://". $_SERVER['HTTP_HOST'] . $location);
    // }
}

function mysql2date($dateformatstring, $mysqlstring, $translate = true) {
	global $month, $weekday, $month_abbrev, $weekday_abbrev;
	$m = $mysqlstring;
	if (empty($m)) {
		return false;
	}
	$i = mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4)); 
	if (!empty($month) && !empty($weekday) && $translate) {
		$datemonth = $month[date('m', $i)];
		$datemonth_abbrev = $month_abbrev[$datemonth];
		$dateweekday = $weekday[date('w', $i)];
		$dateweekday_abbrev = $weekday_abbrev[$dateweekday]; 		
		$dateformatstring = ' '.$dateformatstring;
		$dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
		$dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
		$dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
		$dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
	
		$dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
	}
	$j = @date($dateformatstring, $i);
	if (!$j) {
	// for debug purposes
	//	echo $i." ".$mysqlstring;
	}
	return $j;
}

/** mysqlnow()
* Return the current date/time in a format that mysql will be happy
* with. Yes, it's lazy. So sue me. I'm sick of having to remember silly
* date format strings.
* Returns local date/time, not gmtime.
*/
function mysqlnow() {
    return date('Y-m-d H:i:s');
}

/** selectoption( value, label, selected-value );
* Returns an HTML <option>, thus:
* <code>
* <option value="value" SELECTED>label</option>
* </code>
* The SELECTED bit will be there only if value and selected-value are
* identical
* 
* @param string $value
* @param string $label
* @param string $selected
* @return string
*/
function selectoption( $value, $label, $selected='' ) {
	$selected = ($value == $selected) ? ' selected="selected"' : '';
    return '<option value="' . $value . '"' . $selected . '> ' . $label ."</option>\n";
}

/** selectlist( list, selected-value );
* Syntax still in flux
* The idea is to pass in a list of options, and which one is selected,
* and get back a select list with that item selected.
* <code>
* $list = selectlist( array('KY' => 'Kentucky', 'AL' => 'Alabama', 
*                    'SD' => 'South Dakota'), 'KY' );
* </code>
*/
function selectlist( $options, $selected) {
    foreach ($options as $value => $label) {
        $list .= selectoption( $value, $label, $selected );
    }
    return $list;
}

/**
 * The action is always passed in the PATH_INFO, except when it isn't.
 */
function action() {
    if ( isset ( $_SERVER['PATH_INFO'] ) ) {
        $args = explode( '/', $_SERVER['PATH_INFO'] );
        return $args[1];
    } else {
        return   isset( $_GET['action']  ) ?
                        $_GET['action']    :
               ( isset( $_POST['action'] ) ?
                        $_POST['action']   :
                        ''               );
    }
}

function timeadd( $time = '0000', $slot = '0030' ) {

    // The time is in the format 0830, so need to chop it up
    $bits = str_split( $time );
    $h = $bits[0] . $bits[1];
    $m = $bits[2] . $bits[3];
   
    // What's that look like in seconds?
    $start = $h * 60 * 60 + $m * 60;
    // Add the slot length
    $end   = $start + ( $slot * 60 );

    // Put it back into hours and minutes
    $ret   = gmdate( 'Hi', $end );
    return $ret;
}

?>
